
package fileby.model.impl.filemanagement.impl;

import fileby.util.TransferableFileUtil;
import java.awt.datatransfer.Transferable;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 *
 * @author Oscar
 */
class ClipboardManager implements ClipboardOwner {

    private static Clipboard clipboard;
    private static ClipboardManager clipboardManager;

    private ClipboardManager() {
        clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    }

    public static ClipboardManager getInstance() {
        if (clipboardManager == null) {
            clipboardManager = new ClipboardManager();
        }
        return clipboardManager;
    }

    public void setClipboardContent(List<File> fileList) throws IllegalStateException {
        TransferableFileUtil content = new TransferableFileUtil(fileList);
        clipboard.setContents(content, this);
    }

    public List getClipboardContent() throws IllegalStateException, UnsupportedFlavorException, IOException {
        if (clipboard.getContents(this) != null) {
            if (clipboard.isDataFlavorAvailable(DataFlavor.javaFileListFlavor)) {
                Transferable content = clipboard.getContents(this);
                List<File> fileList = (List) content.getTransferData(DataFlavor.javaFileListFlavor);
                return fileList;
            }
        }
        return null;
    }

    //From the ClipboardOwner interface, this method is not implemented.
    public void lostOwnership(Clipboard clipboard, Transferable contents) {}

}
